package top.wnma.crud.web.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import top.wnma.crud.pojo.Department;
import top.wnma.crud.pojo.Staff;
import top.wnma.crud.service.IDepartmentService;
import top.wnma.crud.service.IStaffService;

@Controller
@Scope("prototype")
public class StaffAction extends ActionSupport implements ModelDriven<Staff>{

	private Staff staff=new Staff();
	
	@Autowired
	private IStaffService staffService;
	@Autowired
	private IDepartmentService departmentService;
	
	public Staff getModel() {
		return staff;
	}
	
	public String list() {
		List<Staff> list = staffService.findAll();
		ActionContext.getContext().put("staffList", list);
		return "list";
	}
	public String delete() {
		Integer sid = staff.getSid();
		Staff staff = staffService.findById(sid);
		if(staff!=null) {
			staffService.delete(staff);  
		}
		return "toList";
	}
	public String toEdit() {
		Integer sid = staff.getSid();
		if(sid!=null && sid>=0) {
			Staff staff = staffService.findById(sid);
			
			ActionContext.getContext().put("staff", staff);
		}
		List<Department> departmentList = departmentService.findAll();
		ActionContext.getContext().put("departmentList", departmentList);
		return "Edit";
	}
	public String update() {
		System.out.println(staff+"222222222222222==============="); 
		System.out.println(staff.getDepartment().getDid()+"33333332222222222222==============="); 
		staffService.saveOrUpdate(staff);
		return "toList";
	}

}
